layui.config({
    base: "js/"
}).use(['form', 'layer', 'jquery', 'laypage','laytpl','util','upload'], function () {
    var form = layui.form
        , layer = parent.layer === undefined ? layui.layer : parent.layer
        , laypage = layui.laypage,laytpl = layui.laytpl, $ = layui.jquery,upload = layui.upload;

    dataList(1);

    /**
     * 添加链接
     */
    $("body").on("click", ".addBtn", function () {
        addOrEdit("创建链接", "/m/friendly/link/add", "添加", false);
    });

    /**
     * 编辑链接
     */
    $("body").on("click", ".linkEdit", function () {
        var id = $(this).data("id");
        addOrEdit("编辑链接", "/m/friendly/link/update/" + id, "编辑", true, "/m/friendly/link/" + id);
    })

    /**
     * 查询
     */
    form.on('submit(queryForm)', function(data){
        $("#number").val(1);
        dataList(1);
        return false;
    });

    /**
     * 查询数据列表
     * @param number
     */
    function dataList(number) {
        var options = {
            dataType: "json",
            success: function (data) {
                if(data && data.error) {
                    layer.msg(data.error.msg);
                    return;
                }
                var listData = {"list":data.page.records};
                var getTpl = dataListTpl.innerHTML,view = document.getElementById('listDataBody');

                laytpl(getTpl).render(listData, function(html){
                    view.innerHTML = html;
                });
                form.render();
                if(number === 1) {
                    //分页标签
                    pageBar(data.page.total,data.page.size);
                }
            },
            error: function (e) {
                layer.msg(JSON.stringify(e));
            }
        };
        $("#listForm").ajaxSubmit(options);
    }

    /**
     * 数据分页
     * @param count
     * @param limit
     */
    function pageBar(count,limit) {
        laypage.render({
            elem: "page",
            limit: limit,
            count: count,
            theme: '#1E9FFF',
            layout: ['count', 'prev', 'page', 'next'],
            jump: function(obj, first){
                if(!first){
                    $("#number").val(obj.curr);
                    dataList(obj.curr);
                }
            }
        });
    }


    /**
     * 添加或编辑链接
     * @param title
     * @param postUrl
     * @param yesBtnName
     * @param isEdit
     * @param queryUrl
     */
    function addOrEdit(title, postUrl, yesBtnName, isEdit, queryUrl) {
        var index = layui.layer.open({
            title: title,
            anim: 2,
            area: ['500px', '600px'],
            content: $("#addLink").html(),
            btn: [yesBtnName, '取消'],
            yes: function () {
                var company = $(".company").val(), url = $(".url").val(),pic=$("#pic").val(),
                    tel = $(".tel").val(),desc = $(".desc").val(),sort = $(".sort").val();

                if (!company) {
                    layer.msg("请填写公司名称！", {
                        icon: 5
                    });
                    return;
                }
                if (!url) {
                    layer.msg("请填写链接地址！", {
                        icon: 5
                    });
                    return;
                }
                if (!pic) {
                    layer.msg("请上传logo！", {
                        icon: 5
                    });
                    return;
                }
                post(postUrl, {
                    "company": company,
                    "url": url,
                    "pic":pic,
                    "tel":tel,
                    "desc":desc,
                    "sort":sort
                }, function (data) {
                    if (data.code == 0) {
                        layui.layer.close(index);
                        layer.msg(data.msg || "操作成功~", {icon: 6});
                        dataList(1);
                    }
                })

            },
            success: function () {
                upload.render({
                    elem: '.thumbBox',
                    url: '/up/lay',
                    done: function(res, index, upload){
                        var src = res.data.src;
                        $("#pic").val(src);
                        $('.thumbImg').attr('src',src);
                        $('.thumbBox').css("background","#fff");
                    }
                });
                if (isEdit) {
                    get(queryUrl, null, function (data) {
                        if (data.code == 0) {
                            $('.thumbBox').css("background","#fff");
                            $(".company").val(data.link.company);
                            $(".tel").val(data.link.tel);
                            $('.thumbImg').attr('src',data.link.pic);
                            $("#pic").val(data.link.pic);
                            $(".url").val(data.link.url);
                            $(".desc").val(data.link.desc);
                            $(".sort").val(data.link.sort);
                        }
                    })
                }
            }
        })
    }

})

function updateLinkStatus(id,status) {
    var title = '确定要显示？';
    if(title==1){
        title = '确定要隐藏？';
    }
    layer.confirm(title, {
        icon: 3,
        title: '提示信息'
    }, function (index) {
        post("/m/friendly/link/updateLinkStatus/" + id, {
            "status": status
        }, function (data) {
            if (data.code == 0) {
                layer.msg(data.msg, {icon: 6});
                setTimeout(function () {
                    window.location.reload();
                },1000)

            }
        });
    });


}
